package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import dao.DBUtil;

public class LoginPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] jtf = new JTextField[2];
	private JButton[] jb = new JButton[2];

	public LoginPanel() {

		setOpaque(false);
		setLayout(new GridBagLayout());
		Font font = new Font("微软雅黑", Font.ITALIC, 20);
		String[] s1 = { "用户名", "密码" };
		String[] s2 = { "登录", "注册" };
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 30, 10, 0);
		for (int i = 0; i < s1.length; i++) {
			gbc.anchor = GridBagConstraints.CENTER;
			JLabel jl = new JLabel(s1[i]);
			jl.setFont(font);
			gbc.gridx = 0;
			gbc.gridy = i;
			add(jl, gbc);
			jtf[i] = new JTextField(10);
			jtf[i].setFont(font);
			gbc.gridx = 1;
			add(jtf[i], gbc);
			jb[i] = new JButton(s2[i]);
			jb[i].setFont(font);
			jb[i].addActionListener(this);
			gbc.gridx = i;
			gbc.gridy = 2;
			add(jb[i], gbc);
		}

	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		Composite cs = g2.getComposite();
		g2.setColor(Color.gray);
		g2.setComposite(AlphaComposite.SrcOver.derive(0.9f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(cs);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jb[0]) {
			String sql = "select * from login";
			Statement stm;
			try {
				Connection conn=DBUtil.getConn();
				stm = conn.createStatement();
				ResultSet rs = stm.executeQuery(sql);
				if(rs.next())
				{
				if(jtf[0].getText().trim().equals(rs.getString("username"))&&jtf[1].getText().trim().equals(rs.getString("pwd")))
						{
							this.setVisible(false);
							JOptionPane.showMessageDialog(this, "登录成功");
							if(jtf[0].getText()!=rs.getString("username"))
							{
								JOptionPane.showMessageDialog(this, "用户名输入错误！");
								jtf[0].setText("");
							}
							else
							{
								JOptionPane.showMessageDialog(this, "密码输入错误！");
								jtf[1].setText("");
							}
						}
				if(jtf[0].getText().equals("")||jtf[1].getText().equals("")){
					JOptionPane.showMessageDialog(this, "请输入用户名和密码！");
				}
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}

	}
}
